package com.news.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.news.entity.User;
import com.news.service.UserService;
import com.news.service.UserServiceImpl;
import com.news.util.MD5Util;
import com.news.util.Page;

/**
 * 此Servlet将处理与用户相关的所有请求
 *
 */
@WebServlet({"/saveUser.do","/editUser.do",
			"/deleteUser.do","/deleteUserBatch.do",
			"/getUser.do","/listUser.do",
			"/login.do","/logout.do"})
public class UserController extends HttpServlet{
	private static final long serialVersionUID = -596030381114687653L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1.根据用户的路径判断用户的目的
		String servletPath = req.getServletPath();
		if(servletPath.contains("save")){
			//新增
			saveUser(req, resp);
		}else if(servletPath.contains("edit")){
			//更新
			editUser(req, resp);
		}else if(servletPath.contains("deleteUserBatch")){
			//批量删除
			deleteUserBatch(req, resp);
		}else if(servletPath.contains("deleteUser")){
			//单个删除
			deleteUser(req, resp);
		}else if(servletPath.contains("get")){
			//单个查询
			getUser(req, resp);
		}else if(servletPath.contains("list")){
			//批量查询，分页查询
			listUser(req, resp);
		}else if(servletPath.contains("login")){
			login(req, resp);
		}else if(servletPath.contains("logout")){
			logout(req, resp);
		}
	}
	
	private UserService userService = new UserServiceImpl();
	
	private void saveUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1.获取数据
		String name = req.getParameter("name");
		String acct = req.getParameter("acct");
		String pwd = req.getParameter("pwd");
		//2.将数据封装到实体类对象
		User user = new User();
		user.setName(name);
		user.setAcct(acct);
		user.setPwd(MD5Util.getMD5(pwd));
		//3.调用业务层
		userService.saveUser(user);
		//4.跳转到查询
		resp.sendRedirect("listUser.do");
	}
	
	private void editUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1.获取数据
		String id = req.getParameter("id");
		String name = req.getParameter("name");
		String pwd = req.getParameter("pwd");
		//2.将数据封装到实体类对象
		User user = new User();
		user.setId(Integer.parseInt(id));
		user.setName(name);
		user.setPwd(MD5Util.getMD5(pwd));
		//3.调用业务层
		userService.editUser(user);
		//4.跳转到查询
		resp.sendRedirect("listUser.do");
	}
	
	private void deleteUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1.获取数据
		String id = req.getParameter("id");
		//2.调用业务层
		userService.removeUser(Integer.parseInt(id));
		//3.跳转到查询
		resp.sendRedirect("listUser.do");
	}
	
	private void deleteUserBatch(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1.获取数据
		String[] ids = req.getParameterValues("ids");
		//2.调用业务层
		userService.removeUserBatch(ids);
		//3.跳转到查询
		resp.sendRedirect("listUser.do");
	}
	
	private void getUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1.获取数据
		String id = req.getParameter("id");
		//2.调用业务层
		User user = userService.getUser(Integer.parseInt(id));
		//3.将数据放置在域对象中
		req.setAttribute("user", user);
		//4.跳转
		req.getRequestDispatcher("").forward(req, resp);
	}
	
	private void listUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1.获取数据
		String pageNum = req.getParameter("pageNum")==null?"1":req.getParameter("pageNum");
		String pageSize = req.getParameter("pageSize")==null?"0":req.getParameter("pageSize");
		String acct = req.getParameter("acct");
		//2.调用业务层
		Page page = userService.listUser(Integer.parseInt(pageNum), Integer.parseInt(pageSize), acct);
		//3.将数据放置在域对象中
		req.setAttribute("page", page);
		//4.跳转
		req.getRequestDispatcher("page/user.jsp").forward(req, resp);
	}	
	
	private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1.获取数据
		String acct = req.getParameter("acct");
		String pwd = req.getParameter("pwd");
		//2.调用业务层
		User user = userService.login(acct, MD5Util.getMD5(pwd));
		//3.判断用户是否登录成功
		if(user==null){
			//登录失败
			//提示信息
			req.setAttribute("msg", "用户名或密码有误");
			//跳回登录页面
			req.getRequestDispatcher("login.jsp").forward(req, resp);
		}else{
			//登录成功
			//将用户信息放置在session中
			req.getSession().setAttribute("loginUser", user);
			//跳转到主页
			resp.sendRedirect("index.jsp");
		}
		
	}
	
	private void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//注销session
		req.getSession().invalidate();
		//跳转到登录也
		resp.sendRedirect("login.jsp");
	}
}
